package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.entity.ShoppingCart;
import com.itheima.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @PackageName: com.itheima.controller
 * @ClassName: ShoppingCartController
 * @Description:
 * @Author: zhangcheng
 * @DateTime: 2022/6/13 8:34
 * @Version 1.0
 */

@RestController
@RequestMapping("/shoppingCart")
@Transactional
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> save(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //绑定当前用户id
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        //判断用户点的是菜品还是套餐
        if (shoppingCart.getDishId() != null) {//菜品
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        } else {//套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }

        ShoppingCart one = shoppingCartService.getOne(lqw);
        //第一次点是新增(后面点是修改)
        if (one == null) {
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            one = shoppingCart;
        } else {//不是第一次,更新数据
            one.setNumber(one.getNumber() + 1);
            shoppingCartService.updateById(one);
        }
        return R.success(one);
    }

    /**
     * 查询购物车
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        return R.success(shoppingCartService.list(
                new LambdaQueryWrapper<ShoppingCart>()
                        .eq(ShoppingCart::getUserId, BaseContext.getCurrentId())
                        .orderByAsc(ShoppingCart::getCreateTime)
        ));
    }

    /**
     * 取消购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());//where user_id = ?
        if (shoppingCart.getDishId() != null) {//是菜品
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());//dish_id = ?
        } else {//是套餐
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());//setmeal_id = ?
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        Integer number = one.getNumber();
        number--;
        one.setNumber(number);
        if (number > 0) {
            //更新数据
            shoppingCartService.updateById(one);
        } else if (number == 0) {
            //删除
            shoppingCartService.removeById(one.getId());
        }
        return R.success(one);
    }

    /**
     * 清空购物车
     *
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>()
                .eq(ShoppingCart::getUserId,BaseContext.getCurrentId()));
        return R.success("清空购物车成功");
    }
}
